package com.aranandroid.customview.fragment

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import com.aranandroid.customview.R

/**
 * 描述：
 * -
 * 创建人：ybr
 * 创建时间：2021
 */
class ScrollControlViewPager : ViewPager {
    // 是否可以滑动切换
    var DISABLE = true
    // 是否scrollview嵌套
    var ISSCROLL = false
    set(value) {
        field = value
        invalidate()
    }

    constructor(context: Context?) : super(context!!) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(
        context!!,
        attrs
    ) {}

        override fun onInterceptTouchEvent(arg0: MotionEvent): Boolean {
        return DISABLE && super.onInterceptTouchEvent(arg0)
    }

    override fun onTouchEvent(arg0: MotionEvent): Boolean {
        return DISABLE && super.onTouchEvent(arg0)
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var heightMeasureSpec = heightMeasureSpec
        if(ISSCROLL){
            var height = 0
            for (i in 0 until childCount) {
                val child = getChildAt(i)
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
                val h = child.measuredHeight
                if (h > height) height = h
            }
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

}